import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

export const routes = [  // 把路由的组件渲染到树状组件里面来
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/home",
    name: "home",
    meta: {
      // 元数据
      name: "首页",
    },
    component: HomeView,
    children: [
      {
        // 面包屑  管理员列表
        path: "manage",
        name: "manage",
        label:"账户管理",
        meta: {
          name: "管理员管理",
        },
        component: () => import("../views/user/UserView.vue"),
        children: [
          {
            path: "manageList",
            name: "manageList",
            label:"管理员列表",
            meta: {
              name: "管理员列表",
            },
            component: () => import("../views/user/UserList.vue"),
          },
        ],
      },
      {
        path: "swipeManage",
        name: "swipeManage",
        label:"轮播图管理",
        meta: {
          name: "轮播图管理",
        },
        component: () => import("../views/swipe/SwipeView.vue"),
        children: [
          {
            path: "swipeList",
            name: "swipeList",
            label:"轮播图列表",
            meta: {
              name: "轮播图列表",
            },
            component: () => import("../views/swipe/SwipeList.vue"),
          },
          {
            path:"addSwipe",
            name:"addSwipe",
            label:"添加轮播图",
            meta:{
              name:"添加轮播图"
            },
            component:()=>import("../views/swipe/AddSwipe.vue")
          }
        ],
      },
      {
        // 产品列表
        path: "productManage",
        name: "productManage",
        label:"产品管理",
        meta: {
          name: "产品管理",
        },
        component: () => import("../views/product/productView.vue"),
        children: [
          {
            path: "productList",
            name: "productList",
            label:"产品列表",
            meta: {
              name: "产品列表",
            },
            component: () => import("../views/product/productList.vue"),
          },
          {
            path: "secondKill",
            name: "secondKill",
            label:"秒杀列表",
            meta: {
              name: "秒杀列表",
            },
            component: () => import("../views/product/SecondProduct.vue"),
          },
          {
            path: "tipList",
            name: "tipList",
            label:"推荐列表",
            meta: {
              name: "推荐列表",
            },
            component: () => import("../views/product/TipList.vue"),
          },
          {
            path: "filterList",
            name: "filterList",
            label:"筛选列表",
            meta: {
              name: "筛选列表",
            },
            component: () => import("../views/product/FilterList.vue"),
          },
        ],
      },
      {
        path: "editor",
        name: "editor",
        label:"编辑器",
        meta: {
          name: "编辑器",
        },
        component: () => import("../views/editor/EditorView.vue"),
        children: [
          {
            path: "editorDemo",
            name: "editorDemo",
            label:"富文本编辑器",
            meta: {
              name: "富文本编辑器",
            },
            component: () => import("../views/editor/EditorDemo.vue"),
          },
          
        ],
      },
      {
        path: "echartsManage",
        name: "echartsManage",
        label:"数据可视化",
        meta: {
          name: "数据可视化",
        },
        component: () => import("../views/echarts/EchartsView.vue"),
        children: [
          {
            path: "echartsShow",
            name: "echartsShow",
            label:"echarts数据展示",
            meta: {
              name: "echarts数据展示",
            },
            component: () => import("../views/echarts/EchartsShow.vue"),
          },
          
        ],
      },
      {
        path: "map",
        name: "map",
        label:"地图",
        meta: {
          name: "地图",
        },
        component: () => import("../views/map/MapView.vue"),
        children: [
          {
            path: "myMap",
            name: "myMap",
            label:"我的地图",
            meta: {
              name: "我的地图",
            },
            component: () => import("../views/map/MyMap.vue"),
          },
          
        ],
      },
    ],
  },
  {
    path: "/about",
    name: "about",
    component: () => import("../views/AboutView.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

// 前置路由守卫 ----》 登录防跳功能
router.beforeEach((to, from) => {
  if (to.fullPath === "/login") {
    // 去登录页面，返回true
    if (localStorage.getItem("token")) {
      // 判断是否已经登录，已经登录，直接跳转到home首页
      return { path: "/home" };
    } else {
      return true; // 还没有登录
    }
  } else {
    // 不是去登录页面，判断是否已经登录
    if (localStorage.getItem("token")) {
      // 已经登录了，直接跳转
      return true;
    } else {
      return { path: "/login" }; // 如果没有登录，跳转到登录页面
    }
  }
});

export default router;
